package com.nantong.admin.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import com.nantong.admin.annotation.PermissionRegister;
import com.nantong.admin.domain.SysUser;
import com.nantong.admin.domain.model.request.user.SysUserSearch;
import com.nantong.admin.domain.model.response.user.SysUserVo;
import com.nantong.admin.service.ISysUserService;
import com.nantong.common.core.domain.R;
import com.nantong.common.mybatis.core.page.PageQuery;
import com.nantong.common.mybatis.core.page.TableDataInfo;

import org.springframework.web.bind.annotation.*;

/**
 * 用户管理
 *
 * @author nantong
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/system/user")
@PermissionRegister(
    name = "用户管理", 
    key = "system:user", 
    type = 2,  // 2=菜单
    path = "user",
    component = "/views/User/UserView.vue",
    icon = "user",
    sortOrder = 1,
    parentKey = "system"  // 父菜单是system
)
@Tag(name = "用户管理", description = "用户管理相关接口")
public class SysUserController {

    private final ISysUserService userService;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取用户列表", description = "获取系统用户列表")
    @SaCheckPermission("system:user:list")
    @PermissionRegister(
            name = "用户列表查询",
            key = "system:user:list",
            type = 3,  // 3=按钮
            remark = "查询用户数据列表"
    )
    public R<List<SysUserVo>> list(SysUserSearch search) {
        List<SysUserVo> users = userService.searchList(search);
        return R.ok(users);
    }

    /**
     * 获取用户列表
     */
    @SaCheckPermission("system:user:page")
    @GetMapping("/page")
    @PermissionRegister(
            name = "用户分页查询",
            key = "system:user:page",
            type = 3,  // 3=按钮
            remark = "查询用户数据分页"
    )
    public TableDataInfo<SysUserVo> page(SysUserSearch search, PageQuery pageQuery) {
        return userService.selectPageList(search, pageQuery);
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/{userId}")
    @Operation(summary = "获取用户详情", description = "根据用户ID获取用户详情")
    @SaCheckPermission("system:user:query")
    @PermissionRegister(
            name = "用户详情查询",
            key = "system:user:query",
            type = 3,  // 3=按钮
            remark = "查询用户数据详情"
    )
    public R<SysUser> getInfo(@PathVariable Long userId) {
        SysUser user = userService.getUserWithRoles(userId);
        return R.ok(user);
    }

    /**
     * 新增用户
     */
    @PostMapping
    @Operation(summary = "新增用户", description = "新增系统用户")
    @SaCheckPermission("system:user:add")
    @PermissionRegister(
            name = "用户新增",
            key = "system:user:add",
            type = 3,  // 3=按钮
            remark = "新增用户数据"
    )
    public R<Void> add(@RequestBody SysUser user) {
        userService.saveUserWithRoles(user);
        return R.ok();
    }

    /**
     * 修改用户
     */
    @PutMapping
    @Operation(summary = "修改用户", description = "修改系统用户")
    @SaCheckPermission("system:user:edit")
    @PermissionRegister(
            name = "用户修改",
            key = "system:user:edit",
            type = 3,  // 3=按钮
            remark = "修改用户数据"
    )
    public R<Void> edit(@RequestBody SysUser user) {
        userService.updateUserWithRoles(user);
        return R.ok();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{userId}")
    @Operation(summary = "删除用户", description = "删除系统用户")
    @SaCheckPermission("system:user:delete")
    @PermissionRegister(
            name = "用户删除",
            key = "system:user:delete",
            type = 3,  // 3=按钮
            remark = "删除用户数据"
    )
    public R<Void> remove(@PathVariable Long userId) {
        userService.removeById(userId);
        return R.ok();
    }
}
